package com.tenglong.teacher.data.resultInfo;

import android.text.TextUtils;

import com.cuotibao.zhuoxiao.basics.utils.IpUtils;
import com.netease.nimlib.p.a;
import com.netease.nimlib.sdk.msg.attachment.AudioAttachment;
import com.netease.nimlib.sdk.msg.attachment.FileAttachment;
import com.netease.nimlib.sdk.msg.attachment.ImageAttachment;
import com.netease.nimlib.sdk.msg.attachment.MsgAttachment;
import com.netease.nimlib.sdk.msg.constant.AttachStatusEnum;
import com.netease.nimlib.sdk.msg.constant.MsgDirectionEnum;
import com.netease.nimlib.sdk.msg.constant.MsgStatusEnum;
import com.netease.nimlib.sdk.msg.constant.MsgTypeEnum;
import com.netease.nimlib.sdk.msg.constant.SessionTypeEnum;
import com.netease.nimlib.sdk.msg.model.CustomMessageConfig;
import com.netease.nimlib.sdk.msg.model.IMMessage;
import com.netease.nimlib.sdk.msg.model.MemberPushOption;
import com.netease.nimlib.sdk.msg.model.NIMAntiSpamOption;
import com.tenglong.teacher.ui.answerq.P2PRecord;

import java.util.Map;

public class NeteaseIM extends a {
    private String eventtype;
    private String convtype;
    private String to;
    private String fromaccount;
    private String fromclienttype;
    private String fromdeviceid;
    private String fromnick;
    private String msgtimestamp;
    private String msgtype;
    private String body;
    private String attach;
    private String msgidclient;
    private String msgidserver;
    private String resendflag;
    private String customsafeflag;
    private String customapnstext;
    private MsgAttachment attachment;

    public void setEventtype(String eventtype) {
        this.eventtype = eventtype;
    }

    public void setConvtype(String convtype) {
        this.convtype = convtype;
    }

    public void setTo(String to) {
        this.to = to;
    }

    public void setFromaccount(String fromaccount) {
        this.fromaccount = fromaccount;
    }

    public void setFromclienttype(String fromclienttype) {
        this.fromclienttype = fromclienttype;
    }

    public void setFromdeviceid(String fromdeviceid) {
        this.fromdeviceid = fromdeviceid;
    }

    public void setFromnick(String fromnick) {
        this.fromnick = fromnick;
    }

    public void setMsgtimestamp(String msgtimestamp) {
        this.msgtimestamp = msgtimestamp;
    }

    public void setMsgtype(String msgtype) {
        this.msgtype = msgtype;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public void setAttach(String attach) {
        this.attach = attach;
    }

    public void setMsgidclient(String msgidclient) {
        this.msgidclient = msgidclient;
    }

    public void setMsgidserver(String msgidserver) {
        this.msgidserver = msgidserver;
    }

    public void setResendflag(String resendflag) {
        this.resendflag = resendflag;
    }

    public void setCustomsafeflag(String customsafeflag) {
        this.customsafeflag = customsafeflag;
    }

    public void setCustomapnstext(String customapnstext) {
        this.customapnstext = customapnstext;
    }

    public void setTmembers(String tmembers) {
        this.tmembers = tmembers;
    }

    public void setExt(String ext) {
        this.ext = ext;
    }

    public void setAntispam(String antispam) {
        this.antispam = antispam;
    }

    public void setYidunres(String yidunres) {
        this.yidunres = yidunres;
    }

    private String tmembers;
    private String ext;
    private String antispam;
    private String yidunres;

    public String getEventtype() {
        return eventtype;
    }

    public String getConvtype() {
        return convtype;
    }

    public String getTo() {
        return to;
    }

    public String getFromaccount() {
        return fromaccount;
    }

    public String getFromclienttype() {
        return fromclienttype;
    }

    public String getFromdeviceid() {
        return fromdeviceid;
    }

    public String getFromnick() {
        return fromnick;
    }

    public String getMsgtimestamp() {
        return msgtimestamp;
    }

    public String getMsgtype() {
        return msgtype;
    }

    public String getBody() {
        return body;
    }

    public String getAttach() {

        return attach;
    }

    public String getMsgidclient() {
        return msgidclient;
    }

    public String getMsgidserver() {
        return msgidserver;
    }

    public String getResendflag() {
        return resendflag;
    }

    public String getCustomsafeflag() {
        return customsafeflag;
    }

    public String getCustomapnstext() {
        return customapnstext;
    }

    public String getTmembers() {
        return tmembers;
    }

    public String getExt() {
        return ext;
    }

    public String getAntispam() {
        return antispam;
    }

    public String getYidunres() {
        return yidunres;
    }



    @Override
    public String getUuid() {
        return msgidclient;
    }

    @Override
    public boolean isTheSame(IMMessage imMessage) {
        if(getUuid().equals(imMessage.getUuid())) {
            return true;
        }else{
            return false;
        }
    }

    @Override
    public String getSessionId() {
        return getTo();
    }

    @Override
    public SessionTypeEnum getSessionType() {
        return SessionTypeEnum.P2P;
    }

    @Override
    public String getFromNick() {
        return null;
    }

    @Override
    public MsgTypeEnum getMsgType() {
        if(msgtype.equals("PICTURE")){
            return MsgTypeEnum.image;
        }else if(msgtype.equals("AUDIO")){
            return MsgTypeEnum.audio;
        }else if (msgtype.equals("TEXT")){
            return MsgTypeEnum.text;
        } else {
            return super.getMsgType();
        }
    }


    @Override
    public MsgStatusEnum getStatus() {
        return MsgStatusEnum.read;
    }

    @Override
    public void setStatus(MsgStatusEnum msgStatusEnum) {

    }

    @Override
    public void setDirect(MsgDirectionEnum msgDirectionEnum) {

    }

    @Override
    public MsgDirectionEnum getDirect() {
        if (p2PRecord!=null){
            return getNewsDirect(p2PRecord);
        } else {
            return getOldDirect();
        }
    }

    private P2PRecord p2PRecord;
    public NeteaseIM setP2PRecord(P2PRecord p2PRecord){
        this.p2PRecord = p2PRecord;
        return this;
    }

    private String tempImtoken;
    public MsgDirectionEnum getNewsDirect(P2PRecord p2PRecord) {
        if (p2PRecord != null){
            tempImtoken = p2PRecord.fromaccount;
        } else {
            tempImtoken = DataState.getInstance().getTempImtoken();
        }
        return getDire(p2PRecord);
    }

    private MsgDirectionEnum getDire(P2PRecord p2PRecord){
        if (!DataState.getInstance().getUserInfo().getImUserName().equals(fromaccount) && !DataState.getInstance().getUserInfo().getImUserName().equals(to)) {
            if (tempImtoken == null) {
                if (p2PRecord != null&& TextUtils.isEmpty(p2PRecord.fromaccount)){
                    p2PRecord.fromaccount = fromaccount;
                } else {
                    DataState.getInstance().setTempImtoken(fromaccount);
                }
                tempImtoken = fromaccount;
                return MsgDirectionEnum.In;
            } else {
                if (p2PRecord.fromaccount.equals(fromaccount)) {
                    return MsgDirectionEnum.In;
                } else {
                    return MsgDirectionEnum.Out;
                }
            }
        } else {
            if (DataState.getInstance().getUserInfo().getImUserName().equals(fromaccount)) {
                return MsgDirectionEnum.Out;
            } else {
                return MsgDirectionEnum.In;
            }
        }
    }


    private MsgDirectionEnum getOldDirect(){
        if (!DataState.getInstance().getUserInfo().getImUserName().equals(fromaccount) && !DataState.getInstance().getUserInfo().getImUserName().equals(to)) {
            if (DataState.getInstance().getTempImtoken() == null) {
                DataState.getInstance().setTempImtoken(fromaccount);
                return MsgDirectionEnum.Out;
            } else {
                if (DataState.getInstance().getTempImtoken().equals(fromaccount)) {
                    return MsgDirectionEnum.Out;
                } else {
                    return MsgDirectionEnum.In;
                }
            }
        } else {
            if (DataState.getInstance().getUserInfo().getImUserName().equals(fromaccount)) {
                return MsgDirectionEnum.Out;
            } else {
                return MsgDirectionEnum.In;
            }
        }
    }
    public boolean isMySendMessage(){
        if(DataState.getInstance().getUserInfo().getImUserName().equals(fromaccount)){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public void setContent(String s) {

    }

    @Override
    public String getContent() {
        return getBody();
    }

    @Override
    public long getTime() {
        return Long.parseLong(getMsgtimestamp());
    }

    @Override
    public void setFromAccount(String s) {

    }

    @Override
    public String getFromAccount() {
        return getFromaccount();
    }

    @Override
    public String toString() {
        return "NeteaseIM{" +
                "eventtype='" + eventtype + '\'' +
                ", convtype='" + convtype + '\'' +
                ", to='" + to + '\'' +
                ", fromaccount='" + fromaccount + '\'' +
                ", fromclienttype='" + fromclienttype + '\'' +
                ", fromdeviceid='" + fromdeviceid + '\'' +
                ", fromnick='" + fromnick + '\'' +
                ", msgtimestamp='" + msgtimestamp + '\'' +
                ", msgtype='" + msgtype + '\'' +
                ", body='" + body + '\'' +
                ", attach='" + attach + '\'' +
                ", msgidclient='" + msgidclient + '\'' +
                ", msgidserver='" + msgidserver + '\'' +
                ", resendflag='" + resendflag + '\'' +
                ", customsafeflag='" + customsafeflag + '\'' +
                ", customapnstext='" + customapnstext + '\'' +
                ", tmembers='" + tmembers + '\'' +
                ", ext='" + ext + '\'' +
                ", antispam='" + antispam + '\'' +
                ", yidunres='" + yidunres + '\'' +
                '}';
    }

    @Override
    public void setAttachment(MsgAttachment msgAttachment) {

    }

    @Override
    public MsgAttachment getAttachment() {
        if (attachment!=null){
            return attachment;
        }
        if(msgtype.equals("PICTURE")){
            ImageAttachment image = new ImageAttachment(attach);
            String url = image.getUrl();
            if ((url.contains("https://")||url.contains("http://"))&&url.startsWith("/neteaseim/")){
                image.setUrl(url.replace("/neteaseim/",""));
            } else {
                image.setUrl(IpUtils.getImageUrl(image.getUrl()));
            }
            attachment = image;
            return image;
        }else if(msgtype.equals("AUDIO")){
            AudioAttachment audio = new AudioAttachment(attach);
            String url = audio.getUrl();
            if ((url.contains("https://")||url.contains("http://"))&&url.startsWith("/neteaseim/")){
                audio.setUrl(url.replace("/neteaseim/",""));
            } else {
                audio.setUrl(IpUtils.getImageUrl(audio.getUrl()));
            }
            attachment = audio;
            return audio;
        }
        attachment = new FileAttachment(attach);
        return attachment;
    }

    @Override
    public AttachStatusEnum getAttachStatus() {
        return AttachStatusEnum.transferred;
    }

    @Override
    public void setAttachStatus(AttachStatusEnum attachStatusEnum) {

    }

    @Override
    public CustomMessageConfig getConfig() {
        return null;
    }

    @Override
    public void setConfig(CustomMessageConfig customMessageConfig) {

    }

    @Override
    public Map<String, Object> getRemoteExtension() {
        return null;
    }

    @Override
    public void setRemoteExtension(Map<String, Object> map) {

    }

    @Override
    public Map<String, Object> getLocalExtension() {
        return null;
    }

    @Override
    public void setLocalExtension(Map<String, Object> map) {

    }

    @Override
    public String getPushContent() {
        return resendflag;
    }

    @Override
    public void setPushContent(String s) {
        resendflag=s;

    }

    @Override
    public Map<String, Object> getPushPayload() {
        return null;
    }

    @Override
    public void setPushPayload(Map<String, Object> map) {

    }

    @Override
    public MemberPushOption getMemberPushOption() {
        return null;
    }

    @Override
    public void setMemberPushOption(MemberPushOption memberPushOption) {

    }

    @Override
    public boolean isRemoteRead() {
        return false;
    }

    @Override
    public boolean needMsgAck() {
        return false;
    }

    @Override
    public void setMsgAck() {

    }

    @Override
    public boolean hasSendAck() {
        return false;
    }

    @Override
    public int getTeamMsgAckCount() {
        return 0;
    }

    @Override
    public int getTeamMsgUnAckCount() {
        return 0;
    }

    @Override
    public int getFromClientType() {
        return 0;
    }

    @Override
    public NIMAntiSpamOption getNIMAntiSpamOption() {
        return null;
    }

    @Override
    public void setNIMAntiSpamOption(NIMAntiSpamOption nimAntiSpamOption) {

    }

    @Override
    public void setClientAntiSpam(boolean b) {

    }

    @Override
    public void setForceUploadFile(boolean b) {

    }
}
